﻿package com.amcharts.chartClasses
{
    import com.amcharts.*;
    import com.amcharts.events.*;
    import com.amcharts.geom.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.net.*;
    import mx.collections.*;
    import mx.core.*;
    import mx.events.*;
    import mx.formatters.*;
    import mx.styles.*;

    public class AmChart extends UIComponent
    {
        protected var _scrolRect:Rectangle;
        protected var _chartCreated:Boolean;
        protected var _dispatchDataChanged:Boolean = false;
        protected var _previousWidth:Number;
        protected var _dataProvider:IList;
        protected var _previousHeight:Number;
        protected var _prefixesOfBigNumbers:Array;
        protected var _usePrefixes:Boolean;
        protected var _legend:AmLegend;
        protected var _prefixesOfSmallNumbers:Array;
        protected var _dataProviderChanged:Boolean = false;
        protected var _filters:Array;
        protected var _numberFormatter:NumberFormatter;
        protected var _marginTop:Number;
        protected var _chartDataProvider:ArrayCollection;
        protected var _clipContent:Boolean = true;
        protected var _marginRight:Number;
        protected var _marginBottom:Number;
        protected var _percentFormatter:NumberFormatter;
        protected var _dataChanged:Boolean = false;
        protected var _rolledOver:Boolean;
        protected var _balloon:AmBalloon;
        protected var _marginLeft:Number;
        protected var _amchartsLink:Sprite;
        private static var stylesInited:Boolean = initStyles();
        static const DEFAULT_MEASURED_HEIGHT:Number = 400;
        static const VERSION:String = "1.8.3.1";
        static const DEFAULT_MEASURED_WIDTH:Number = 500;

        public function AmChart()
        {
            _prefixesOfBigNumbers = [{number:1000, prefix:"k"}, {number:1000000, prefix:"M"}, {number:1000000000, prefix:"G"}, {number:1000000000000, prefix:"T"}, {number:1000000000000000, prefix:"P"}, {number:1000000000000000000, prefix:"E"}, {number:1000000000000000000000, prefix:"Z"}, {number:999999999999999980000000, prefix:"Y"}];
            _prefixesOfSmallNumbers = [{number:1e-024, prefix:"y"}, {number:1e-021, prefix:"z"}, {number:1e-018, prefix:"a"}, {number:1e-015, prefix:"f"}, {number:1e-012, prefix:"p"}, {number:1e-009, prefix:"n"}, {number:1e-006, prefix:"μ"}, {number:0.001, prefix:"m"}];
            tabEnabled = false;
            _dataProvider = new ArrayCollection();
            _chartDataProvider = new ArrayCollection();
            _numberFormatter = new NumberFormatter();
            _percentFormatter = new NumberFormatter();
            _percentFormatter.rounding = NumberBaseRoundType.NEAREST;
            _percentFormatter.precision = 1;
            _scrolRect = new Rectangle(0, 0, DEFAULT_MEASURED_WIDTH, DEFAULT_MEASURED_HEIGHT);
            this.addEventListener(MouseEvent.MOUSE_OVER, handleMouseOver);
            this.addEventListener(MouseEvent.MOUSE_OUT, handleMouseOut);
            return;
        }// end function

        public function set balloon(param1:AmBalloon) : void
        {
            if (_balloon)
            {
                _balloon.destroy();
                removeChild(_balloon);
            }
            _balloon = param1;
            addChild(_balloon);
            return;
        }// end function

        protected function designMode() : void
        {
            return;
        }// end function

        public function set numberFormatter(param1:NumberFormatter) : void
        {
            _numberFormatter = param1;
            return;
        }// end function

        public function invalidateData() : void
        {
            _dataChanged = true;
            invalidateProperties();
            return;
        }// end function

        protected function dispatchDataUpdatedEvent() : void
        {
            if (!_chartCreated)
            {
                updateLegend();
                if (_dataProvider.length > 0)
                {
                }
                if (!_dataProviderChanged)
                {
                }
                if (_dispatchDataChanged)
                {
                    _dataProviderChanged = false;
                    _dispatchDataChanged = false;
                    _dataChanged = false;
                    dispatchEvent(new AmChartEvent(AmChartEvent.DATA_UPDATED));
                }
            }
            return;
        }// end function

        public function get legend() : AmLegend
        {
            return _legend;
        }// end function

        protected function updateData() : void
        {
            return;
        }// end function

        public function get legendDataProvider() : ArrayCollection
        {
            return new ArrayCollection();
        }// end function

        public function get clipContent() : Boolean
        {
            return _clipContent;
        }// end function

        public function get numberFormatter() : NumberFormatter
        {
            return _numberFormatter;
        }// end function

        override protected function createChildren() : void
        {
            super.createChildren();
            if (UIComponentGlobals.designMode)
            {
                callLater(designMode);
            }
            if (!_balloon)
            {
                _balloon = new AmBalloon();
                addChild(_balloon);
            }
            _amchartsLink = new UIComponent();
            var _loc_1:* = new Rect(140, 22, [16777215], [1]);
            _amchartsLink.addChild(_loc_1);
            var _loc_2:* = IUITextField(createInFontContext(UITextField));
            _loc_2.width = 137;
            _loc_2.height = 18;
            _loc_2.x = 4;
            _loc_2.y = 2;
            _amchartsLink.buttonMode = true;
            _amchartsLink.mouseChildren = false;
            _amchartsLink.addChild(DisplayObject(_loc_2));
            _amchartsLink.addEventListener(MouseEvent.MOUSE_UP, getAmCharts);
            _loc_2.htmlText = "<font color=\'#000000\' size=\'10\'>chart by amCharts.com</font>";
            addChild(_amchartsLink);
            return;
        }// end function

        public function set legend(param1:AmLegend) : void
        {
            if (_legend)
            {
                _legend.removeEventListener(LegendEvent.SHOW_ITEM, handleLegendEvent);
                _legend.removeEventListener(LegendEvent.HIDE_ITEM, handleLegendEvent);
                _legend.removeEventListener(LegendEvent.CLICK_MARKER, handleLegendEvent);
                _legend.removeEventListener(LegendEvent.CLICK_LABEL, handleLegendEvent);
                _legend.removeEventListener(LegendEvent.ROLL_OVER_ITEM, handleLegendEvent);
                _legend.removeEventListener(LegendEvent.ROLL_OUT_ITEM, handleLegendEvent);
                _legend.removeEventListener(LegendEvent.ROLL_OVER_MARKER, handleLegendEvent);
                _legend.removeEventListener(LegendEvent.ROLL_OUT_MARKER, handleLegendEvent);
            }
            _legend = param1;
            _legend.addEventListener(LegendEvent.SHOW_ITEM, handleLegendEvent);
            _legend.addEventListener(LegendEvent.HIDE_ITEM, handleLegendEvent);
            _legend.addEventListener(LegendEvent.CLICK_MARKER, handleLegendEvent);
            _legend.addEventListener(LegendEvent.CLICK_LABEL, handleLegendEvent);
            _legend.addEventListener(LegendEvent.ROLL_OVER_ITEM, handleLegendEvent);
            _legend.addEventListener(LegendEvent.ROLL_OUT_ITEM, handleLegendEvent);
            _legend.addEventListener(LegendEvent.ROLL_OVER_MARKER, handleLegendEvent);
            _legend.addEventListener(LegendEvent.ROLL_OUT_MARKER, handleLegendEvent);
            return;
        }// end function

        protected function showError(param1:String) : void
        {
            return;
        }// end function

        protected function updateAmChartsLink() : void
        {
            if (_amchartsLink)
            {
                this.setChildIndex(_amchartsLink, (this.numChildren - 1));
            }
            return;
        }// end function

        protected function getAmCharts(event:MouseEvent) : void
        {
            var _loc_2:* = new URLRequest("http://flex.amcharts.com/?utm_source=swc&utm_medium=demo&utm_campaign=flexChartDemo");
            navigateToURL(_loc_2, "_blank");
            return;
        }// end function

        public function get prefixesOfSmallNumbers() : Array
        {
            return _prefixesOfSmallNumbers;
        }// end function

        public function get chartCreated() : Boolean
        {
            return _chartCreated;
        }// end function

        public function get balloon() : AmBalloon
        {
            return _balloon;
        }// end function

        protected function handleMouseOut(event:Event) : void
        {
            _rolledOver = false;
            return;
        }// end function

        public function get percentFormatter() : NumberFormatter
        {
            return _percentFormatter;
        }// end function

        protected function updateLegend() : void
        {
            dispatchEvent(new Event("legendDataChanged"));
            return;
        }// end function

        public function set percentFormatter(param1:NumberFormatter) : void
        {
            _percentFormatter = param1;
            return;
        }// end function

        public function get dataProvider() : Object
        {
            return _dataProvider;
        }// end function

        public function set clipContent(param1:Boolean) : void
        {
            _clipContent = param1;
            return;
        }// end function

        public function set prefixesOfSmallNumbers(param1:Array) : void
        {
            _prefixesOfSmallNumbers = param1;
            return;
        }// end function

        protected function handleLegendEvent(event:LegendEvent) : void
        {
            return;
        }// end function

        protected function dataChangedHandler(event:CollectionEvent) : void
        {
            _dataChanged = true;
            _dispatchDataChanged = true;
            invalidateProperties();
            return;
        }// end function

        public function set usePrefixes(param1:Boolean) : void
        {
            _usePrefixes = param1;
            invalidateProperties();
            return;
        }// end function

        override protected function commitProperties() : void
        {
            super.commitProperties();
            _chartCreated = false;
            if (filters.length > 0)
            {
                _filters = filters;
            }
            this.filters = [];
            if (_dataChanged == true)
            {
                updateData();
                _dataChanged = false;
            }
            invalidateDisplayList();
            return;
        }// end function

        public function set dataChanged(param1:Boolean) : void
        {
            _dataChanged = param1;
            return;
        }// end function

        protected function updateDataStyles() : void
        {
            return;
        }// end function

        public function get rolledOver() : Boolean
        {
            return _rolledOver;
        }// end function

        public function get prefixesOfBigNumbers() : Array
        {
            return _prefixesOfBigNumbers;
        }// end function

        public function get chartDataProvider() : ArrayCollection
        {
            return _chartDataProvider;
        }// end function

        public function get usePrefixes() : Boolean
        {
            return _usePrefixes;
        }// end function

        protected function handleMouseOver(event:Event) : void
        {
            _rolledOver = true;
            return;
        }// end function

        override protected function measure() : void
        {
            super.measure();
            this.measuredWidth = DEFAULT_MEASURED_WIDTH;
            this.measuredHeight = DEFAULT_MEASURED_HEIGHT;
            return;
        }// end function

        public function set prefixesOfBigNumbers(param1:Array) : void
        {
            _prefixesOfBigNumbers = param1;
            return;
        }// end function

        public function set dataProvider(param1:Object) : void
        {
            if (!param1)
            {
                param1 = new ArrayCollection();
            }
            if (param1)
            {
            }
            if (!(param1 is Array))
            {
            }
            if (!(param1 is IList))
            {
                showError("The dataProvider must be Array, ArrayCollection or IList");
            }
            if (param1 is Array)
            {
                _dataProvider = new ArrayCollection(param1 as Array);
            }
            if (param1 is IList)
            {
                _dataProvider = IList(param1);
            }
            _dataProvider.removeEventListener("collectionChange", dataChangedHandler);
            _dataProvider.addEventListener("collectionChange", dataChangedHandler);
            _dataChanged = true;
            _dataProviderChanged = true;
            invalidateProperties();
            return;
        }// end function

        override protected function updateDisplayList(param1:Number, param2:Number) : void
        {
            super.updateDisplayList(param1, param2);
            if (_chartDataProvider.length == 0)
            {
                showError("no data found");
                updateLegend();
                _balloon.hide();
                return;
            }
            _balloon.setBounds(3, 3, this.width - 4, this.height - 4);
            _scrolRect.width = this.width;
            _scrolRect.height = this.height;
            if (_clipContent)
            {
                this.scrollRect = _scrolRect;
            }
            return;
        }// end function

        private static function initStyles() : Boolean
        {
            var styleDeclaration:* = StyleManager.getStyleDeclaration("AmChart");
            if (!styleDeclaration)
            {
                styleDeclaration = new CSSStyleDeclaration();
                StyleManager.setStyleDeclaration("AmChart", styleDeclaration, false);
            }
            styleDeclaration.defaultFactory = function () : void
            {
                return;
            }// end function
            ;
            return true;
        }// end function

    }
}
